//
//  UIView+zj.swift
//  ZJSwiftKit
//
//  Created by zyx on 2023/2/16.
//

import Foundation


// MARK: - Porperties

extension ZJPrefix where Base: UIView {
    
    /// 视图截图 
    public var screenshot: UIImage? {
        UIGraphicsBeginImageContextWithOptions(base.layer.frame.size, false, 0)
        defer {
            UIGraphicsEndImageContext()
        }
        
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        base.layer.render(in: context)
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}


// MARK: - Method
extension ZJPrefix where Base: UIView {
    
    /// 从视图集合中往View添加子视图
    /// - Parameter views: 子视图集合
    public func addViews(_ views: [UIView]) {
        views.forEach {
            base.addSubview($0)
        }
    }
    
    /// 从 XIB 加载View
    /// - Parameters:
    ///   - name: nib name.
    ///   - bundle: bundle of nib (default is nil). 组件中使用的话话一定要传入组件的Bundle
    /// - Returns: 返回ixbView 可能不存在为nil
    public static func loadFromNib(named name: String, bundle: Bundle? = nil) -> UIView? {
        return UINib(nibName: name, bundle: bundle).instantiate(withOwner: nil, options: nil)[0] as? UIView
    }
    
    /// 从XIB加载某个类型的视图
    /// - Parameters:
    ///   - name: nib name
    ///   - bundle: bundle of nib (default is nil). 组件中使用的话话一定要传入组件的Bundle
    /// - Returns: 返回 view 类型为当前调用方法的视图类型
    public static func loadFromNib<T: UIView>(withClass name: T.Type, bundle: Bundle? = nil) -> T {
        let named = String(describing: name)
        guard let view = UINib(nibName: named, bundle: bundle).instantiate(withOwner: nil, options: nil)[0] as? T else {
            fatalError("First element in xib file \(named) is not of type \(named)")
        }
        return view
    }
    
    /// 给 view 设置部分圆角或者全部圆角
    /// - Parameters:
    ///   - corners: 需要改变的角 `(example: [.bottomLeft, .topRight]).`
    ///   - radius: 半径.
    public func addRoundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let maskPath = UIBezierPath(
            roundedRect: base.bounds,
            byRoundingCorners: corners,
            cornerRadii: CGSize(width: radius, height: radius))

        let shape = CAShapeLayer()
        shape.path = maskPath.cgPath
        base.layer.mask = shape
    }
    
    /// 快速添加与父视图的间距约束
    ///```
    ///Example:
    ///let brownView = UIView.init()
    /// brownView.backgroundColor = .brown
    /// ⚠️:必须先添加到父视图才能调用此方法
    ///yellowView.addSubview(brownView)
    ///brownView.zj.fillToSuperview(contentEdgeInsets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))`
    ///```
    /// - Parameter contentEdgeInsets: 与父视图的内容间距 默认上下左右都是 0
    public func fillToSuperview(contentEdgeInsets: UIEdgeInsets? = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) ) {
        
        base.translatesAutoresizingMaskIntoConstraints = false
        if let superview = base.superview {
            
            let leftSpace = contentEdgeInsets?.left ?? 0
            let topSpace = contentEdgeInsets?.top ?? 0
            let rightSpace = contentEdgeInsets?.right ?? 0
            let bottomSpace = contentEdgeInsets?.bottom ?? 0
            
            let left = base.leftAnchor.constraint(equalTo: superview.leftAnchor, constant: leftSpace)
            let right = base.rightAnchor.constraint(equalTo: superview.rightAnchor, constant: -rightSpace)
            let top = base.topAnchor.constraint(equalTo: superview.topAnchor, constant: topSpace)
            let bottom = base.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: -bottomSpace)
            NSLayoutConstraint.activate([left, right, top, bottom])
        }
        
    }
    
    /// 清空视图
    public func removeSubviews() {
        
        base.subviews.forEach { $0.removeFromSuperview() }
    }
    
}
